package net.neptech.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.neptech.mapper.InstanceAssignUserRecordMapper;
import net.neptech.model.dto.InstanceAssignUserRecordParamDto;
import net.neptech.enums.workflow.InstanceAssignUserRecordStatusEnum;
import net.neptech.model.workflow.InstanceAssignUserRecord;
import net.neptech.service.InstanceAssignUserRecordService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author Saya
 * @Date 2025/8/20 12:06
 * @Description TODO
 **/
@Service
public class InstanceAssignUserRecordServiceImpl extends ServiceImpl<InstanceAssignUserRecordMapper,InstanceAssignUserRecord> implements InstanceAssignUserRecordService {

    /**
     * 设置执行人
     */
    @Override
    public void createTaskEvent(InstanceAssignUserRecordParamDto instanceAssignUserRecordParamDto) {
        {
            //在委派或者转办时 会有一个任务 不同的执行人 所以要更新状态为从进行中为已结束
            this.lambdaUpdate()
                    .eq(InstanceAssignUserRecord::getTaskId, instanceAssignUserRecordParamDto.getTaskId())
                    .eq(InstanceAssignUserRecord::getExecutionId, instanceAssignUserRecordParamDto.getExecutionId())
                    .eq(InstanceAssignUserRecord::getStatus, InstanceAssignUserRecordStatusEnum.ON_GOING.getCode())
                    .set(InstanceAssignUserRecord::getStatus,
                            InstanceAssignUserRecordStatusEnum.ENDED.getCode())
                    .set(InstanceAssignUserRecord::getTaskType,
                            instanceAssignUserRecordParamDto.getTaskType())
                    .set(InstanceAssignUserRecord::getEndTime,new Date())
                    .update(new InstanceAssignUserRecord());
        }

        //新增一条数据
        InstanceAssignUserRecord processInstanceAssignUserRecord = BeanUtil.copyProperties(instanceAssignUserRecordParamDto, InstanceAssignUserRecord.class);
        processInstanceAssignUserRecord.setStartTime(new Date());
        processInstanceAssignUserRecord.setStatus(InstanceAssignUserRecordStatusEnum.ON_GOING.getCode());
        processInstanceAssignUserRecord.setTaskType("");
        this.save(processInstanceAssignUserRecord);
    }

    /**
     * 任务完成通知
     */
    @Override
    public void taskCompletedEvent(InstanceAssignUserRecordParamDto instanceAssignUserRecordParamDto) {
        this.lambdaUpdate()
                .eq(InstanceAssignUserRecord::getTaskId, instanceAssignUserRecordParamDto.getTaskId())
                .eq(InstanceAssignUserRecord::getUserId, instanceAssignUserRecordParamDto.getUserId())
                .eq(InstanceAssignUserRecord::getProcessInstanceId, instanceAssignUserRecordParamDto.getProcessInstanceId())
                .eq(InstanceAssignUserRecord::getStatus, InstanceAssignUserRecordStatusEnum.ON_GOING.getCode())
                .set(!instanceAssignUserRecordParamDto.getAuto(), InstanceAssignUserRecord::getStatus,
                        InstanceAssignUserRecordStatusEnum.ENDED.getCode())
                .set(instanceAssignUserRecordParamDto.getAuto(), InstanceAssignUserRecord::getStatus,
                        InstanceAssignUserRecordStatusEnum.PENDING.getCode())
                .set(InstanceAssignUserRecord::getEndTime, new Date())
                .set(InstanceAssignUserRecord::getData, instanceAssignUserRecordParamDto.getData())
                .set(InstanceAssignUserRecord::getLocalData, instanceAssignUserRecordParamDto.getLocalData())
                .set(InstanceAssignUserRecord::getTaskType, instanceAssignUserRecordParamDto.getTaskType())
                .set(InstanceAssignUserRecord::getAuto, instanceAssignUserRecordParamDto.getAuto())
                .update(new InstanceAssignUserRecord());
    }

    /**
     * 任务结束
     */
    @Override
    public void taskEndEvent(InstanceAssignUserRecordParamDto instanceAssignUserRecordParamDto) {
        InstanceAssignUserRecord processInstanceAssignUserRecord = this.lambdaQuery()
                .eq(InstanceAssignUserRecord::getTaskId, instanceAssignUserRecordParamDto.getTaskId())
                .eq(InstanceAssignUserRecord::getUserId, instanceAssignUserRecordParamDto.getUserId())
                .eq(InstanceAssignUserRecord::getProcessInstanceId, instanceAssignUserRecordParamDto.getProcessInstanceId())
                .eq(InstanceAssignUserRecord::getStatus, InstanceAssignUserRecordStatusEnum.ENDED.getCode())
                .one();
        if (processInstanceAssignUserRecord != null) {
            processInstanceAssignUserRecord.setStatus(InstanceAssignUserRecordStatusEnum.PENDING.getCode());
            processInstanceAssignUserRecord.setEndTime(new Date());
            processInstanceAssignUserRecord.setData(instanceAssignUserRecordParamDto.getData());
            processInstanceAssignUserRecord.setLocalData(instanceAssignUserRecordParamDto.getLocalData());
            processInstanceAssignUserRecord.setTaskType(instanceAssignUserRecordParamDto.getTaskType());
            this.updateById(processInstanceAssignUserRecord);
        }
    }

    /**
     * 清理数据
     */
    @Override
    public void clearProcess(List<String> flowIdList) {
        this.lambdaUpdate()
                .in(InstanceAssignUserRecord::getFlowId, flowIdList)
                .remove();
    }
}